﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using CCWin;
using BookModel;
using System.Text.RegularExpressions;
using BLL;

namespace WinFromBook
{
    public partial class FrmReaderID : CCSkinMain
    {
        public FrmReaderID()
        {
            InitializeComponent();
        }
        #region 封装
        ReaderInfoBLL bll = new ReaderInfoBLL();
        public void insertReading()
        {
            if (textName.Text == "")
            {
                MessageBox.Show("用户名不能为空", "系统提示！");
                return;
            }
            if ((!Regex.IsMatch(textID.Text, @"^[1-9]\d{5}[1-9]9\d{4}3[0-1]\d{4}$
                                 |^[1-9]\d{5}[1-9]9\d{4}[0-2][0-9]\d{4}$|
                                  ^[1-9]\d{5}[1-9]9\d{4}3[0-1]\d{3}X%
                                   |^[1-9]\d{5}[1-9]9\d{4}[0-2][0-9]\d{3}X$", RegexOptions.IgnoreCase)))
            {
                MessageBox.Show("请输入正确的身份证号码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;

            }
            if (!Regex.IsMatch(textPhone.Text, @"13[012356789]\d{8}|15[012356789]\d{8}|18[012356789]\d{8}"))
            {
                MessageBox.Show("请输入正确的手机号码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (this.textAge.Text.Trim() == "")
            {
                MessageBox.Show("请正确输入您的年龄", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (Convert.ToInt32(this.textAge.Text.Trim()) <= 0 || Convert.ToInt32(this.textAge.Text.Trim()) > 100)
            {
                MessageBox.Show("请正确输入您的年龄", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            ReaderInfo read = new ReaderInfo();
            read.ReaderName = textName.Text;
            read.ReaderSex = this.radioM.Checked ? "女" : "男";
            read.ReaderCard = this.textID.Text;
            read.ReaderPhone = this.textPhone.Text;
            read.ReaderAge = Convert.ToInt16(this.textAge.Text);
            read.Readeremark = this.textReark.Text;

            string sql = bll.ADDReadInfo(read);

            MessageBox.Show(sql, "系统提示");
            selectReadInfo();
            TextClear();
        }
        public void selectReadInfo()
        {
            List<ReaderInfo> list = bll.GetReaderInfo();
            this.dataGridRead.DataSource = list.ToList();
        }
        public void TextClear()
        {
            this.textName.Text = "";
            this.radioF.Text = "";
            this.textID.Text = "";
            this.textPhone.Text = "";
            this.textAge.Text = "";
            this.textReark.Text = "";
        }
        #endregion

        private void buttRead_Click(object sender, EventArgs e)
        {
            insertReading();
        }

        private void buttClear_Click(object sender, EventArgs e)
        {
            TextClear();
        }

        private void FrmReaderID_Load(object sender, EventArgs e)
        {
            selectReadInfo();
        }
        //检测账号输入
        public bool TextCheck(KeyPressEventArgs e)
        {
            //数字，大写英文，小写英文，删除键
            if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar >= 'A' && e.KeyChar <= 'Z') || (e.KeyChar >= 'a' && e.KeyChar <= 'z') || ((Keys)(e.KeyChar) == Keys.Back))
            {
                return false;
            }
            return true;

        }
        private void textID_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = TextCheck(e);
        }

        private void textPhone_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = TextCheck(e);
        }

        private void textAge_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = TextCheck(e);
        }

    }
}
